package com.xx.sg.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xx.sg.own.entity.CommunicationTerminalLog;
import com.xx.sg.own.service.CommunicationTerminalLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/communicationTerminalLog")
@Tag(name = "对讲机日志记录", description = "对讲机API")
public class CommunicationTerminalLogController {

    @Autowired
    private CommunicationTerminalLogService terminalLogService;


    @GetMapping("/list")
    @Operation(summary = "获取对讲机", description = "获取对讲机")

    public List<CommunicationTerminalLog> list(String devId) {
        LambdaQueryWrapper<CommunicationTerminalLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommunicationTerminalLog::getDevId, devId)
                .orderByDesc(CommunicationTerminalLog::getCreateTime);
        return terminalLogService.list(wrapper);
    }


    @GetMapping("/timeList")
    @Operation(summary = "获取对讲机时间点之后的数据列表", description = "获取对讲机时间点之后的数据列表")
    public List<CommunicationTerminalLog> timeList(String devId, String startTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime time = LocalDateTime.parse(startTime, formatter);
        LambdaQueryWrapper<CommunicationTerminalLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommunicationTerminalLog::getDevId, devId)
                .ge(CommunicationTerminalLog::getCreateTime, time)
                .orderByAsc(CommunicationTerminalLog::getCreateTime);
        return terminalLogService.list(wrapper);
    }

    @GetMapping("/page")
    @Operation(summary = "分页获取对讲机", description = "分页获取对讲机")
    public Page<CommunicationTerminalLog> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size, String devId) {

        LambdaQueryWrapper<CommunicationTerminalLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommunicationTerminalLog::getDevId, devId)
                .orderByDesc(CommunicationTerminalLog::getCreateTime);

        Page<CommunicationTerminalLog> page = new Page<>(current, size);
        terminalLogService.page(page, wrapper);
        return page;
    }


}
